package com.jy.adscm.operation.mscyb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqbzPartsassembly;
import com.jy.adscm.operation.mscyb.bean.QueryJqbzPartsassembly;
import com.jy.adscm.operation.mscyb.service.JqbzPartsassemblyService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2018-11-26
 */
@RestController
@RequestMapping("/adscm/operation/jqbzPartsassembly")
public class JqbzPartsassemblyController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    JqbzPartsassemblyService jqbzPartsassemblyService;
    @Autowired
    JyCodeMapper jyCodeMapper;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody JqbzPartsassembly jqbzPartsassembly, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        jqbzPartsassembly.setTaskreleaseperson(token.getUser().getId().toString());
        jqbzPartsassembly.setTaskreleaseaddtime(new Date());
        jqbzPartsassembly.setTaskfinishstate(0);
        jqbzPartsassembly.setTaskstate(0);
        jqbzPartsassembly.setIstaskstart("0");
        jqbzPartsassembly.setIstaskbegin("0");
        jqbzPartsassembly.setIstaskend("0");
        jqbzPartsassembly.setIsreviewtask("0");
        jqbzPartsassembly.setReviewresult("-1");
        boolean insert = jqbzPartsassemblyService.insertJqbzPartsassembly(jqbzPartsassembly);
        return new SimpleCoreResponse("200","新增成功",insert);
    }

    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer id)throws AdscmSystemException {
        boolean result = jqbzPartsassemblyService.deleteByIdJqbzPartsassembly(id);
        return new  SimpleCoreResponse("200","删除成功",result);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody JqbzPartsassembly jqbzPartsassembly, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean result = jqbzPartsassemblyService.updateJqbzPartsassembly(jqbzPartsassembly,token);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("selectJqbzPartsassembly")
    public  SimpleCoreResponse  selectLjzzPartsmade(@RequestBody QueryJqbzPartsassembly queryJqbzPartsassembly, HttpServletRequest request) throws  AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Page<JqbzPartsassembly> page =new Page<>(queryJqbzPartsassembly.getCurrentPage(),queryJqbzPartsassembly.getPageSize());
        page = jqbzPartsassemblyService.selectJqbzPartsassembly(page,queryJqbzPartsassembly,token);
        return  new SimpleCoreResponse("200","查询成功",page);
    }
    @RequestMapping("selectUnqualifiedParts")
    public  SimpleCoreResponse  selectUnqualifiedParts(@RequestBody QueryJqbzPartsassembly queryJqbzPartsassembly) throws  AdscmSystemException{
        Page<JqbzPartsassembly> page =new Page<>(queryJqbzPartsassembly.getCurrentPage(),queryJqbzPartsassembly.getPageSize());
        page = jqbzPartsassemblyService.selectUnqualifiedParts(page,queryJqbzPartsassembly);
        return  new SimpleCoreResponse("200","查询成功",page);
    }

    @RequestMapping("selectTaskPerformPerson")
    public  SimpleCoreResponse  selectTaskPerformPerson() throws  AdscmSystemException{
        List<JyUser> users = jqbzPartsassemblyService.selectTaskPerformPerson();
        return  new SimpleCoreResponse("200","查询成功",users);
    }
    @RequestMapping("selectJqMbParts")
    public  SimpleCoreResponse  selectJqMbParts() throws  AdscmSystemException{
        Wrapper<JyCode> wrapper =new EntityWrapper<>();
        wrapper.eq("code_type","1");
        List<JyCode> jyCodes = jyCodeMapper.selectList(wrapper);
        return  new SimpleCoreResponse("200","查询成功",jyCodes);
    }


}

